<?php
namespace App\Admin\Metrics\Home;

use App\Wen\Utils\Settings;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;

class HomeDashboard
{
    public static function title()
    {
        global $__MINI_GLOBAL_TENANT_ID__;
        if($__MINI_GLOBAL_TENANT_ID__ > 0){
            return view('admin/dashboard/title', [
                'version_tip' => '',
                'version_bt_url' => Settings::get('main_site_bt_address', 'https://doc.minisns.cn/doc/112/'),
                'version'=>get_mini_version_code(),
                'logo'=>Settings::get('tenant_admin_avatar', 'https://mini.minisns.cn/statics/mlogo.svg'),
                'name'=>Settings::get('tenant_admin_name', 'MiniSNS'),
                'site' => Settings::get('tenant_admin_site', 'https://www.minisns.cn'),
                'agent'=>Settings::get('tenant_admin_agent', 'https://doc.minisns.cn/doc/99/'),
                'doc'=>Settings::get('tenant_admin_doc', 'https://doc.minisns.cn'),
                'customized'=>Settings::get('tenant_admin_customized', 'https://doc.minisns.cn/doc/132/')
            ]);
        }else{
            return view('admin/dashboard/title', [
                'version_tip' => self::version_tip(),
                'version_bt_url' => Settings::get('main_site_bt_address', 'https://doc.minisns.cn/doc/112/'),
                'version'=>get_mini_version_code(),
                'logo'=>Settings::get('main_site_admin_avatar', 'https://mini.minisns.cn/statics/mlogo.svg'),
                'name'=>Settings::get('main_site_admin_name', 'MiniSNS'),
                'site' => Settings::get('main_site_admin_site', 'https://www.minisns.cn'),
                'doc'=>Settings::get('main_site_admin_doc', 'https://doc.minisns.cn'),
                'agent'=>Settings::get('main_site_admin_agent', 'https://doc.minisns.cn/doc/99/'),
                'customized'=>Settings::get('main_site_admin_customized', 'https://doc.minisns.cn/doc/132/')
            ]);
        }

    }

    public static function version_tip()
    {
        return Cache::remember('system:remote:version:tip', 60, function (){
            try {
                $response  = Http::withHeaders([
                    'device' => 'pc',
                ])->get('https://mini.minisns.cn/api/v1/three/version');
                if($response->successful()){
                    $res = $response->json();
                    $version_str = $res['data'];
                    $remote_version = get_mini_version_code($version_str);
                    $r = _version_compare($remote_version, get_mini_version_code());
                    if($r == '>'){
                        return $remote_version;
                    }
                }
            }catch (\Exception $e){
                return '';
            }
            return '';
        });
    }
}
